package pinyinJava.executors;

import net.sourceforge.pinyin4j.PinyinHelper;
import net.sourceforge.pinyin4j.format.HanyuPinyinCaseType;
import net.sourceforge.pinyin4j.format.HanyuPinyinOutputFormat;
import net.sourceforge.pinyin4j.format.HanyuPinyinToneType;
import net.sourceforge.pinyin4j.format.HanyuPinyinVCharType;
import net.sourceforge.pinyin4j.format.exception.BadHanyuPinyinOutputFormatCombination;
import pinyinJava.bean.DBean;
import pinyinJava.persistence.IDbPersistence;
import pinyinJava.persistence.SqlitePer;

import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;

public class HbExecutor {

    public void execute() throws SQLException {
        String tName = "words";
        File file = new File("pinyin-db", "zy\\ZhuYinWords.db0");
//        String tName = "phrases";
//        File file = new File("pinyin-db", "zy\\ZhuYinPhrases.db0");
        File ph = new File("pinyin-db", "zy\\dict.db0");
        IDbPersistence dbPersistence = new SqlitePer();
        Sim2TraExecutor sim2TraExecutor = new Sim2TraExecutor();
        ArrayList<String> list = read(file, dbPersistence);
        HanyuPinyinOutputFormat format = new HanyuPinyinOutputFormat();
        format.setToneType(HanyuPinyinToneType.WITHOUT_TONE);
        format.setCaseType(HanyuPinyinCaseType.LOWERCASE);
        format.setVCharType(HanyuPinyinVCharType.WITH_V);
        Connection connection = dbPersistence.open(ph.getAbsolutePath());
        ArrayList<DBean> all = new ArrayList<>();
        int size = 0;
        int realSize = 0;
        for (String s : list) {
            ArrayList<DBean> ds = read1(s, file, dbPersistence);
            size += ds.size();
            for (DBean d : ds) {
                try {
                    String s1 = PinyinHelper.toHanYuPinyinString(d.getWord(), format, null, false);
                    d.setZyCode(d.getCode());
                    d.setPyCode(s1);
                } catch (BadHanyuPinyinOutputFormatCombination badHanyuPinyinOutputFormatCombination) {
                    badHanyuPinyinOutputFormatCombination.printStackTrace();
                }
                //先判断是否有重复
                boolean have = false;
                for (DBean d1 : all) {
                    if (d1.getZyCode().equals(d.getZyCode()) && d1.getWord().equals(d.getWord())) {
                        have = true;
                        break;
                    }
                }
                if (!have) {
                    realSize++;
                    all.add(d);
                }
            }
        }
        dbPersistence.save(connection, tName, all);
        dbPersistence.close(connection);
        System.out.println("原数据总量：" + size);
        System.out.println("去重后总量：" + realSize);
        System.out.println("done");
    }

    private ArrayList<String> read(File file, IDbPersistence dbPersistence) throws SQLException {
        if (!file.getName().endsWith(".db0")) return null;
        Connection connection = dbPersistence.open(file.getAbsolutePath());
        ArrayList<String> list = dbPersistence.tables(connection);
        dbPersistence.close(connection);
        return list;
    }

    private ArrayList<DBean> read1(String tName, File file, IDbPersistence dbPersistence) throws SQLException {
        if (!file.getName().endsWith(".db0")) return null;
        Connection connection = dbPersistence.open(file.getAbsolutePath());
        ArrayList<DBean> list = dbPersistence.read(connection, tName);
        dbPersistence.close(connection);
        return list;
    }

}
